﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UM_SERVER.Session
{
    /// <summary>
    ///    会话管理器
    /// </summary>
    class SessionManager : Manager
    {
        /// <summary>
        ///     覆盖指定用户会话
        /// </summary>
        /// <param name="NewUser">新用户</param>
        /// <returns>返回null , 则表示覆盖失败</returns>
        public ISession CoverSession(User.User NewUser)
        {
            for (int i = 0; i < this.Collection.Count; i++)
            {
                if (this.Collection[i].Key == NewUser.Key)
                {
                    //原来会话失效
                    (this.Collection[i] as Session).Exprid = true;
                    //强行让原来用户下线
                    (this.Collection[i] as Session).GetUser().SetOffline((this.Collection[i] as Session));
                    BaseSession baseSession =  new BaseSession(NewUser.Key, DateTime.MinValue, NewUser);
                    this.Collection[i] = baseSession;
                    return baseSession;
                }
            }
            return null;
        }

        /// <summary>
        ///      根据指定会话码得到会话对象
        /// </summary>
        /// <param name="SessionCode">会话码</param>
        /// <returns>返回null , 则表示当前不存在该会话</returns>
        public ISession GetSessionByCode(String SessionCode)
        {
            foreach (IMetaData data in Collection)
            {
                if (((ISession)data).GetSessioCode() == SessionCode)
                {
                    return ((ISession) data);
                }
            }
            return null;
        }
    }
}
